﻿@model ProductSearchModel

@using Nop.Core.Domain.Catalog;

@{
    //page title
    ViewBag.PageTitle = T("Admin.Catalog.Products").Text;
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("Products");
}

@{
    const string hideSearchBlockAttributeName = "ProductListPage.HideSearchBlock";
    var hideSearchBlock = genericAttributeService.GetAttribute<bool>(workContext.CurrentCustomer, hideSearchBlockAttributeName);
}

    <form asp-controller="Product" asp-action="List" method="post">
        <div class="content-header clearfix">
            <h1 class="pull-left">
                @T("Admin.Catalog.Products")
            </h1>
            <div class="pull-right">
                <a asp-action="Create" class="btn bg-blue">
                    <i class="fa fa-plus-square"></i>
                    @T("Admin.Common.AddNew")
                </a>
                <button asp-action="DownloadCatalogPDF" type="submit" name="download-catalog-pdf" class="btn bg-purple">
                    <i class="fa fa-file-pdf-o"></i>
                    @T("Admin.Catalog.Products.List.DownloadPDF")
                </button>
                <div class="btn-group">
                    <button type="button" class="btn btn-success">
                        <i class="fa fa-download"></i>
                        @T("Admin.Common.Export")
                    </button>
                    <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                        <span class="caret"></span>
                        <span class="sr-only">&nbsp;</span>
                    </button>
                    <ul class="dropdown-menu" role="menu">
                        <li>
                            <button asp-action="ExportToXml" type="submit" name="exportxml-all">
                                <i class="fa fa-file-code-o"></i>
                                @T("Admin.Common.ExportToXml.All")
                            </button>
                        </li>
                        <li>
                            <button type="button" id="exportxml-selected">
                                <i class="fa fa-file-code-o"></i>
                                @T("Admin.Common.ExportToXml.Selected")
                            </button>
                        </li>
                        <li class="divider"></li>
                        <li>
                            <button asp-action="ExportToExcel" type="submit" name="exportexcel-all">
                                <i class="fa fa-file-excel-o"></i>
                                @T("Admin.Common.ExportToExcel.All")
                            </button>
                        </li>
                        <li>
                            <button type="button" id="exportexcel-selected">
                                <i class="fa fa-file-excel-o"></i>
                                @T("Admin.Common.ExportToExcel.Selected")
                            </button>
                        </li>
                    </ul>
                </div>
                @if (!Model.IsLoggedInAsVendor || Model.AllowVendorsToImportProducts)
                {
                    //a vendor cannot import products
                    <button type="button" name="importexcel" class="btn bg-olive" data-toggle="modal" data-target="#importexcel-window">
                        <i class="fa fa-upload"></i>
                        @T("Admin.Common.Import")
                    </button>
                }
                <button type="button" id="delete-selected" class="btn bg-red">
                    <i class="fa fa-trash-o"></i>
                    @T("Admin.Common.Delete.Selected")
                </button>
                <nop-action-confirmation asp-button-id="delete-selected" />
                @await Component.InvokeAsync("AdminWidget", new { widgetZone = AdminWidgetZones.ProductListButtons })
            </div>
        </div>

        <div class="content">
            <div class="form-horizontal">
                <div class="panel-group">
                    <div class="panel panel-default panel-search">
                        <div class="panel-body">
                            <div class="row search-row @(!hideSearchBlock ? "opened" : "")" data-hideAttribute="@hideSearchBlockAttributeName">
                                <div class="search-text">@T("Admin.Common.Search")</div>
                                <div class="icon-search"><i class="fa fa-search" aria-hidden="true"></i></div>
                                <div class="icon-collapse"><i class="fa fa-angle-down" aria-hidden="true"></i></div>
                            </div>

                            <div class="search-body @(hideSearchBlock ? "closed" : "")">
                                <div class="row">
                                    <div class="col-md-5">
                                        <div class="form-group">
                                            <div class="col-md-4">
                                                <nop-label asp-for="SearchProductName" />
                                            </div>
                                            <div class="col-md-8">
                                                <nop-editor asp-for="SearchProductName" />
                                            </div>
                                        </div>
                                        <div class="form-group" @(Model.AvailableCategories.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                            <div class="col-md-4">
                                                <nop-label asp-for="SearchCategoryId" />
                                            </div>
                                            <div class="col-md-8">
                                                <nop-select asp-for="SearchCategoryId" asp-items="Model.AvailableCategories" />
                                            </div>
                                        </div>
                                        <div class="form-group" @(Model.AvailableCategories.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                            <div class="col-md-4">
                                                <nop-label asp-for="SearchIncludeSubCategories" />
                                            </div>
                                            <div class="col-md-8">
                                                <nop-editor asp-for="SearchIncludeSubCategories" />
                                            </div>
                                        </div>
                                        <div class="form-group" @(Model.AvailableManufacturers.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                            <div class="col-md-4">
                                                <nop-label asp-for="SearchManufacturerId" />
                                            </div>
                                            <div class="col-md-8">
                                                <nop-select asp-for="SearchManufacturerId" asp-items="Model.AvailableManufacturers" />
                                            </div>
                                        </div>
                                        <div class="form-group" @(Model.AvailableVendors.SelectionIsNotPossible() || Model.IsLoggedInAsVendor ? Html.Raw("style='display: none;'") : null)>
                                            <div class="col-md-4">
                                                <nop-label asp-for="SearchVendorId" />
                                            </div>
                                            <div class="col-md-8">
                                                <nop-select asp-for="SearchVendorId" asp-items="Model.AvailableVendors" />
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-7">
                                        <div class="form-group" @(Model.HideStoresList ? Html.Raw("style=\"display:none\"") : null)>
                                            <div class="col-md-4">
                                                <nop-label asp-for="SearchStoreId" />
                                            </div>
                                            <div class="col-md-8">
                                                <nop-select asp-for="SearchStoreId" asp-items="Model.AvailableStores" />
                                            </div>
                                        </div>
                                        <div class="form-group" @(Model.AvailableWarehouses.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                            <div class="col-md-4">
                                                <nop-label asp-for="SearchWarehouseId" />
                                            </div>
                                            <div class="col-md-8">
                                                <nop-select asp-for="SearchWarehouseId" asp-items="Model.AvailableWarehouses" />
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <div class="col-md-4">
                                                <nop-label asp-for="SearchProductTypeId" />
                                            </div>
                                            <div class="col-md-8">
                                                <nop-select asp-for="SearchProductTypeId" asp-items="Model.AvailableProductTypes" />
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <div class="col-md-4">
                                                <nop-label asp-for="SearchPublishedId" />
                                            </div>
                                            <div class="col-md-8">
                                                <nop-select asp-for="SearchPublishedId" asp-items="Model.AvailablePublishedOptions" />
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <div class="col-md-4">
                                                <nop-label asp-for="GoDirectlyToSku" />
                                            </div>
                                            <div class="col-md-8">
                                                <div class="input-group input-group-short">
                                                    <nop-editor asp-for="GoDirectlyToSku" />
                                                    <span class="input-group-btn">
                                                        <button type="submit" id="go-to-product-by-sku" name="go-to-product-by-sku" class="btn btn-info btn-flat">
                                                            @T("Admin.Common.Go")
                                                        </button>
                                                    </span>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-7 col-md-offset-5">
                                        <button type="button" id="search-products" class="btn btn-primary btn-search">
                                            <i class="fa fa-search"></i>
                                            @T("Admin.Common.Search")
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                <div class="panel panel-default">
                    <div class="panel-body">
                        @await Html.PartialAsync("Table", new DataTablesModel
                        {
                            Name = "products-grid",
                            UrlRead = new DataUrl("ProductList", "Product", null),
                            SearchButtonId = "search-products",
                            Length = Model.PageSize,
                            LengthMenu = Model.AvailablePageSizes,
                            Filters = new List<FilterParameter>
                            {
                                new FilterParameter(nameof(Model.SearchProductName)),
                                new FilterParameter(nameof(Model.SearchCategoryId)),
                                new FilterParameter(nameof(Model.SearchIncludeSubCategories), typeof(bool)),
                                new FilterParameter(nameof(Model.SearchManufacturerId)),
                                new FilterParameter(nameof(Model.SearchStoreId)),
                                new FilterParameter(nameof(Model.SearchWarehouseId)),
                                new FilterParameter(nameof(Model.SearchVendorId)),
                                new FilterParameter(nameof(Model.SearchProductTypeId)),
                                new FilterParameter(nameof(Model.SearchPublishedId))
                            },
                            ColumnCollection = new List<ColumnProperty>
                            {
                                new ColumnProperty(nameof(ProductModel.Id))
                                {
                                    IsMasterCheckBox = true,
                                    Render = new RenderCheckBox("checkbox_products"),
                                    ClassName =  NopColumnClassDefaults.CenterAll,
                                    Width = "50"
                                },
                                new ColumnProperty(nameof(ProductModel.PictureThumbnailUrl))
                                {
                                    Title = T("Admin.Catalog.Products.Fields.PictureThumbnailUrl").Text,
                                    Width = "100",
                                    Render = new RenderPicture()
                                },
                                new ColumnProperty(nameof(ProductModel.Name))
                                {
                                    Title = T("Admin.Catalog.Products.Fields.Name").Text
                                },
                                new ColumnProperty(nameof(ProductModel.Sku))
                                {
                                    Title = T("Admin.Catalog.Products.Fields.Sku").Text,
                                    Width = "100"
                                },
                                new ColumnProperty(nameof(ProductModel.Price))
                                {
                                    Title = T("Admin.Catalog.Products.Fields.Price").Text,
                                    Width = "150",
                                    Render = new RenderCustom("renderColumnPrice")
                                },
                                new ColumnProperty(nameof(ProductModel.StockQuantityStr))
                                {
                                    Title = T("Admin.Catalog.Products.Fields.StockQuantity").Text,
                                    Width = "100"
                                },
                                new ColumnProperty(nameof(ProductModel.ProductTypeName))
                                {
                                    Title = T("Admin.Catalog.Products.Fields.ProductType").Text,
                                    Width = "100"
                                },
                                new ColumnProperty(nameof(ProductModel.Published))
                                {
                                    Title = T("Admin.Catalog.Products.Fields.Published").Text,
                                    Width = "100",
                                    ClassName = NopColumnClassDefaults.CenterAll,
                                    Render = new RenderBoolean()
                                },
                                new ColumnProperty(nameof(ProductModel.Id))
                                {
                                    Title = T("Admin.Common.Edit").Text,
                                    Width = "100",
                                    ClassName =  NopColumnClassDefaults.Button,
                                    Render = new RenderButtonEdit(new DataUrl("Edit"))
                                }
                            }
                        })

                        <script>
                            function renderColumnPrice(data, type, row, meta) {
                                return (row.ProductTypeId != @((int)ProductType.GroupedProduct) ) ? data : null;
                            }

                            $(document).ready(function () {
                                $('#delete-selected-action-confirmation-submit-button').bind('click', function () {
                                    var postData = {
                                        selectedIds: selectedIds
                                    };
                                    addAntiForgeryToken(postData);
                                    $.ajax({
                                        cache: false,
                                        type: "POST",
                                        url: "@(Url.Action("DeleteSelected", "Product"))",
                                        data: postData,
                                        error: function (jqXHR, textStatus, errorThrown) {
                                            $('#deleteSelectedFailed-info').text(errorThrown);
                                            $('#deleteSelectedFailed').click();
                                        },
                                        complete: function (jqXHR, textStatus) {
                                            updateTable('#products-grid');
                                        }
                                    });
                                    $('#delete-selected-action-confirmation').modal('toggle');
                                    return false;
                                });
                            });

                        </script>
                        <nop-alert asp-alert-id="deleteSelectedFailed" />
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        $(document).ready(function () {
            $("#@Html.IdFor(model => model.GoDirectlyToSku)").keydown(function (event) {
                if (event.keyCode === 13) {
                    $("#go-to-product-by-sku").click();
                    return false;
                }
            });
        });
    </script>
</form>

    @*import products form*@
    <div id="importexcel-window" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="importexcel-window-title">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="importexcel-window-title">@T("Admin.Common.ImportFromExcel")</h4>
                </div>
                <form asp-controller="Product" asp-action="ImportExcel" method="post" enctype="multipart/form-data">
                    <div class="form-horizontal">
                        <div class="modal-body">
                            <ul class="common-list">
                                <li>
                                    <em>@T("Admin.Catalog.Products.List.ImportFromExcelTip")</em>
                                </li>
                                <li>
                                    <em>@T("Admin.Common.ImportFromExcel.ManyRecordsWarning")</em>
                                </li>
                            </ul>
                            <div class="form-group">
                                <div class="col-md-2">
                                    <div class="label-wrapper">
                                        <label class="control-label">
                                            @T("Admin.Common.ExcelFile")
                                        </label>
                                    </div>
                                </div>
                                <div class="col-md-10">
                                    <input type="file" id="importexcelfile" name="importexcelfile" class="form-control" />
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="submit" class="btn btn-primary">
                                @T("Admin.Common.ImportFromExcel")
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

    @*export selected (XML). We don't use GET approach because it's limited to 2K-4K chars and won't work for large number of entities*@
    <form asp-controller="Product" asp-action="ExportXmlSelected" method="post" id="export-xml-selected-form">
        <input type="hidden" id="selectedIds" name="selectedIds" value="" />
    </form>

<script>
    $(document).ready(function () {
        $('#exportxml-selected').click(function (e) {
            e.preventDefault();
            var ids = selectedIds.join(",");
            if (!ids) {
                $('#exportXmlSelected-info').text("@T("Admin.Products.NoProducts")");
                $("#exportXmlSelected").click();
            }
            else {
                $('#export-xml-selected-form #selectedIds').val(ids);
                $('#export-xml-selected-form').submit();
                updateTable('#products-grid');
            }
            return false;
        });
    });
</script>
<nop-alert asp-alert-id="exportXmlSelected" />

    @*export selected (Excel). We don't use GET approach because it's limited to 2K-4K chars and won't work for large number of entities*@
    <form asp-controller="Product" asp-action="ExportExcelSelected" method="post" id="export-excel-selected-form">
        <input type="hidden" id="selectedIds" name="selectedIds" value="" />
    </form>

    <script>
        $(document).ready(function () {
            $('#exportexcel-selected').click(function (e) {
                e.preventDefault();
                var ids = selectedIds.join(",");
                if (!ids) {
                    $('#exportExcelSelected-info').text("@T("Admin.Products.NoProducts")");
                    $("#exportExcelSelected").click();
                }
                else {
                    $('#export-excel-selected-form #selectedIds').val(ids);
                    $('#export-excel-selected-form').submit();
                    updateTable('#products-grid');
                }
                return false;
            });
        });
    </script>
<nop-alert asp-alert-id="exportExcelSelected" />